package com.zzty.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzty.dao.UserDao;
import com.zzty.entity.UserAdmin;

/**
 * Servlet implementation class RegisterController
 */
@WebServlet("/RegisterController")
public class RegisterController extends HttpServlet {
    private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		String userName = request.getParameter("name"); 
		String pwd = request.getParameter("pwd"); 
		String pwd1 = request.getParameter("pwd1"); 
		if (userName ==""||pwd ==""||pwd1 =="") {
			request.getRequestDispatcher("fail2.html").forward(request, response);
		} else {
			if(!pwd.equals(pwd1)){
				request.getRequestDispatcher("fail2.html").forward(request, response);
			}
			UserAdmin user = new UserAdmin();
			user.setPwd(pwd);
			user.setUsername(userName);

			UserDao uDao = new UserDao();
			
			boolean f = uDao.addUser(user);

			if (f) {
				request.getRequestDispatcher("index.html").forward(request, response);
			} else {
				request.getRequestDispatcher("fail2.html").forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
